package com.wwwarehouse.contract.orders.import_order;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.contract.adapter.ImportOrdersPagersPagerAdapter;
import com.wwwarehouse.contract.bean.GetShopIdBean;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;


/**
 * 订单导入设置ViewPager
 * Created by ShineF on 2017/8/11 0011.
 */

@Route(path = "/contract/ImportOrdersPagersPagerFragment")
public class ImportOrdersPagersPagerFragment extends BaseFragment implements ElasticScrollView.OnPullListener, CustomViewPagerInternal.OnPageChangeListener {

    private CustomViewPagerInternal mCustomPager = null;
    private CirclePageIndicator mPagerInternal = null;
    private GetShopIdBean mData = null;
    private int mType = 0;
    private List<ImportOrdersPagersFragment> mFragmentList = null;
    private LinearLayout mRlBkg = null;
    private StateLayout mStateLayout = null;
    private boolean isShow = false;
    private TextView mIsEqualsShop = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_import_orders_pagers_pager, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ((BaseCardDeskActivity) mActivity).showBackBt();
        mCustomPager = $(R.id.vp_content);
        mPagerInternal = $(R.id.pager_indicator);
        mIsEqualsShop = $(R.id.tv_is_equals_shop);
        mRlBkg = $(R.id.rl_bkg);
        mStateLayout = $(R.id.sl_state);
        ElasticScrollView mCustomScroll = $(R.id.ecl_custom);
        mCustomScroll.setOnPullListener(this);
        mRlBkg.setVisibility(View.INVISIBLE);
        mIsEqualsShop.setVisibility(View.GONE);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ImportOrdersPagersPagerFragment) {
            mActivity.setTitle(R.string.select_shop);
        }
    }

    @Override
    public void requestDatas() {
        mStateLayout.showProgressView(true);
        mType = 0;
        mFragmentList = new ArrayList<>();
        Bundle typeBundle = getArguments();
        if (typeBundle != null) {
            mType = typeBundle.getInt("type");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("buId", "10001878");
//        map.put("buName", "tester");
//        map.put("buName", "");
        map.put("buName", "店铺");
        map.put("sort", "CREATE_TIME DESC");
//        map.put("sort", "CREATE_TIME");
        map.put("page", "1");
        map.put("size", "9");
        loadData(ContractConstant.GET_IMPORT_ORDERS_SHOP, map, 1);
    }

    public void loadData(String url, Map map, final int what) {
        NoHttpUtils.httpPost(url, map, new NoHttpUtils.OnResponseListener() {
            @Override
            public void onSucceed(CommonClass commonClass, int i) {
                if (what == 1) {
                    if ("0".equals(commonClass.getCode())) {
                        mRlBkg.setVisibility(View.VISIBLE);
                        mData = JSON.parseObject(commonClass.getData().toString(), GetShopIdBean.class);
                        setData();
                    }
                }
            }

            @Override
            public void onFailed(String s, int i) {

            }

            @Override
            public void onFinish(int i) {

            }
        }, what);
    }

    public void setData() {
        if (mData != null) {
            int total = mData.getTotal();
            mFragmentList.clear();
            if (null != mData.getList()) {
                ArrayList<GetShopIdBean.ListBean> list = mData.getList();
                int mPager;
                if (total % 9 == 0) {
                    mPager = total / 9;
                } else {
                    mPager = (int) (total / 9f) + 1;
                }
                for (int i = 0; i < mPager; i++) {
                    ImportOrdersPagersFragment fragment = new ImportOrdersPagersFragment();
                    Bundle bundle = new Bundle();
                    bundle.putParcelableArrayList("data", list);
                    bundle.putInt("type", mType);
                    bundle.putInt("position", i);
                    fragment.setArguments(bundle);
                    mFragmentList.add(fragment);
                }
                if (isAdded()) {
                    ImportOrdersPagersPagerAdapter pagerAdapter = new ImportOrdersPagersPagerAdapter
                            (getChildFragmentManager(), mFragmentList, mActivity);
                    mCustomPager.setAdapter(pagerAdapter);
                    mPagerInternal.setViewPager(mCustomPager);
                    mCustomPager.setOffscreenPageLimit(5);
                    mPagerInternal.setOnPageChangeListener(this);
                    mStateLayout.showContentView();
                    mRlBkg.setVisibility(View.VISIBLE);
                }
                if (list != null && list.size() > 0) {
                    mIsEqualsShop.setVisibility(View.GONE);
                } else {
                    mIsEqualsShop.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    @Override
    public void searchOnRealTimeClick(String text) {
        super.searchOnRealTimeClick(text);
        String searchRuleName;
        if (TextUtils.isEmpty(text)) {
            searchRuleName = "";
        } else {
            searchRuleName = text;
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("buId", "10001878");
//        map.put("buName", "tester");
//        map.put("buName", "");
        map.put("buName", searchRuleName);
//        map.put("sort", "CREATE_TIME DESC");
        map.put("sort", "CREATE_TIME");
        map.put("page", "1");
        map.put("size", "9");
        loadData(ContractConstant.GET_IMPORT_ORDERS_SHOP, map, 1);
    }

    @Override
    public void onDownPull() {

        if (!isShow) {
            ((BaseCardDeskActivity) mActivity).showSearchTitle();
        } else {
            ((BaseCardDeskActivity) mActivity).hideSearchTitle();
            ((BaseCardDeskActivity) mActivity).hideSearchInput();
        }
        isShow = !isShow;
    }

    @Override
    public void onUpPull() {

    }

    @Override
    public void onPause() {
        super.onPause();
        ((BaseCardDeskActivity) mActivity).hideSearchTitle();
        ((BaseCardDeskActivity) mActivity).hideSearchInput();
        isShow = !isShow;
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        ((BaseCardDeskActivity) mActivity).hideSearchTitle();
        ((BaseCardDeskActivity) mActivity).hideSearchInput();
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}
